const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //flag2表示教学能力等三个五星的星星数
    flag2: {
      'a': 0,
      'b': 0,
      'c': 0
    },
    tag2: {
      'a': 0,
      'b': 0,
      'c': 0,
      'd': 0
    },
    datas: {},
    areaTxt: '',
    getID: 0, //记录的id
    status: 0, //status为0是未提交评价，为1为已反馈（0助教已评价 1家长已评价）
    parentJudge: "",  //家长评价的内容
    isSubmitBtnDisabled:false
  },
  changeColor11: function(e) {
    let temID = e.currentTarget.dataset.index;
    var that = this;
    var temFlag = that.data.flag2;
    temFlag[temID] = 1;
    that.setData({
      flag2: temFlag
    });
  },
  changeColor12: function(e) {
    let temID = e.currentTarget.dataset.index;
    var that = this;
    var temFlag = that.data.flag2;
    temFlag[temID] = 2;
    that.setData({
      flag2: temFlag
    });
  },
  changeColor13: function(e) {
    let temID = e.currentTarget.dataset.index;
    var that = this;
    var temFlag = that.data.flag2;
    temFlag[temID] = 3;
    that.setData({
      flag2: temFlag
    });
  },
  changeColor14: function(e) {
    let temID = e.currentTarget.dataset.index;
    var that = this;
    var temFlag = that.data.flag2;
    temFlag[temID] = 4;
    that.setData({
      flag2: temFlag
    });
  },
  changeColor15: function(e) {
    let temID = e.currentTarget.dataset.index;
    var that = this;
    var temFlag = that.data.flag2;
    temFlag[temID] = 5;
    that.setData({
      flag2: temFlag
    });
  },
  textareaInput2: function(e) {
    this.setData({
      areaTxt: e.detail.value
    })
    console.log(e.detail.value)
  },
  selText: function(e) {
    let temID = e.currentTarget.dataset.index;
    let temTag = this.data.tag2;
    if (temTag[temID] == 0) {
      temTag[temID] = 1;
    } else {
      temTag[temID] = 0;
    }
    this.setData({
      tag2: temTag
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.getData();
    var that = this;
    that.setData({
      getID: options.id
    })
    that.getAccessDetail();
  },

  /**
   * 获取汇报评价详情
   */
  getAccessDetail: function() {
    var that = this;
    let paramArr = {
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      id: that.data.getID
    }
  
    httpUtils.getAccessDetail(paramArr).then((res) => {
      that.setData({
        datas: res.data,
        status: res.data.status
      })
      console.log(res.data.status)
      if (res.data.status == 1) {
        //若状态为1，表示家长已评价，把相关信息显示下
        let ability = res.data.ability;
        let attitude = res.data.attitude;
        let word_attitude = res.data.word_attitude;
        let p_judge = res.data.p_judge_content;
        p_judge = p_judge.replace('%', ',');
        that.setData({
          isSubmitBtnDisabled:true,
          parentJudge: p_judge,
          flag2: {
            'a': ability,
            'b': attitude,
            'c': word_attitude
          },
        })
      }

    });
  
    
  },

  //此方法为原来获取详情的接口，废弃不用
  getData: function() {
    let paramArr = {
      // access_token: app.globalData.access_token,
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,

    }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "user/judge?sign=" + sign;
    let classPro = new Promise(
      function(resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {

            resolve(res);
          }
        })
      }
    );

    // let classHis = new Object();
    var that = this;
    classPro.then(function(res) {
      //console.log(res.data);
      if (res.data.code === 1) {
        // classHis = res.data;
        that.setData({
          datas: res.data.data,
          getID: res.data.data.id,

        });
      } else {
        wx.showToast({
          title: res.data.info,
          icon: 'none',
          image: '',
          duration: 1500,
          mask: true,

        })

        setTimeout(function() {
          wx.switchTab({
            url: '../index/index',
          })
        }, 1500)
      }
      //console.log(that.data.datas);
    })
  },


  //提交评价
  submitAnswer1: function() {
    let that = this;
    if (that.data.areaTxt === '' || that.data.flag2.a === 0 ||
      that.data.flag2.b === 0 || that.data.flag2.c === 0) {

      wx.showToast({
        title: '请选择星级和填写评价内容',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let temtag = "";
    if (that.data.tag2.a == 1) {
      temtag = '上课注意力集中';
    }
    if (that.data.tag2.b == 1) {
      if (temtag == "") {
        temtag = '讲课细致';
      } else {
        temtag = temtag + ',' + '讲课细致';
      }
    }
    if (that.data.tag2.c == 1) {
      if (temtag == "") {
        temtag = '备课充分';
      } else {
        temtag = temtag + ',' + '备课充分';
      }
    }
    if (that.data.tag2.d == 1) {
      if (temtag == "") {
        temtag = '讲课生动';
      } else {
        temtag = temtag + ',' + '讲课生动';
      }
    }
    if (temtag == "") {
      temtag = that.data.areaTxt;
    } else {
      temtag = that.data.areaTxt + '%' + temtag;
    }
    let bodyParam = {
      id: that.data.getID,
      ability: that.data.flag2.a,
      attitude: that.data.flag2.b,
      word_attitude: that.data.flag2.c,
      content: temtag,
      // access_token:app.globalData.access_token
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,

    }
    let rs = tool.getSign(bodyParam, app.globalData.key); //返回 bodyParam heaer sign
    let urlStr = app.globalData.apiURL + "user/upjudge?sign=" + rs.sign;

    let getAns = new Promise(function(reslove, reject) {
      wx.request({
        url: urlStr,
        header: rs.header,
        data: rs.bodyParam,
        method: 'post',
        success(res) {
          //console.log(res);
          reslove(res);
        }
      })

    });
    getAns.then(function(res) {
      if (res.data.code !== 1) {
        wx.showToast({
          title: "服务器返回" + res.data.info,
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: "提交成功",
          icon: 'success',
          duration: 1000
        });
        setTimeout(function() {
          wx.switchTab({
            url: '../index/index'
          })
        }, 1000)



      }
    })
  },

  //返回按钮点击
  backBtnClick: function() {
    wx.navigateBack({
      delta: -1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})